/**
 * Created by mac on 2017/5/4.
 */

import java.io.*;
import java.security.*;
import javax.crypto.*;

public class SEnc {
    public static void main(String args[]) throws Exception {
        String s = "当然是选择原谅她啊!";
        FileInputStream f = getFileInputStream();
        ObjectInputStream b = getObjectInputStream(f);
        Key k = (Key) b.readObject();
        Cipher cp = Cipher.getInstance("DESede");
        cp.init(Cipher.ENCRYPT_MODE, k);
        byte ptext[] = s.getBytes("UTF8");
        PrintOut(ptext);
        System.out.println("");
        byte ctext[] = getBytes(cp, ptext);
        PrintOut(ctext);
        FileOutputStream f2 = getFileOutputStream();
        f2.write(ctext);
        System.out.println();
    }

    private static byte[] getBytes(Cipher cp, byte[] ptext) throws IllegalBlockSizeException, BadPaddingException {
        return cp.doFinal(ptext);
    }

    private static FileOutputStream getFileOutputStream() throws FileNotFoundException {
        return new FileOutputStream("SEnc.dat");
    }

    private static ObjectInputStream getObjectInputStream(FileInputStream f) throws IOException {
        return new ObjectInputStream(f);
    }

    private static FileInputStream getFileInputStream() throws FileNotFoundException {
        return new FileInputStream("key1.dat");
    }

    private static void PrintOut(byte[] ptext) {
        for (int i = 0; i < ptext.length; i++) {
            System.out.print(ptext[i] + ",");
        }
    }
}
